package com.jcl.recruit.org.mapper;

import com.jcl.recruit.org.model.Department;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author 刘明新
 * @date 2018/8/2 下午2:15
 */
public interface DepartmentMapper {

    @Select({
            "<script>",
            "select * from department where ",
            "<if test=\"superId != null\">super_id = #{superId} and</if>",
            "<if test=\"subcompanyId != null\">subcompany_id = #{subcompanyId} and</if>",
            "<if test=\"keyword != null\">(fullname like CONCAT('%',#{keyword},'%')  or shortname like CONCAT('%',#{keyword},'%')) and</if>",
            "1 = 1",
            "order by id",
            "</script>"
    })
    List<Department> selectDepartmentListsBySupperIdAndCompanyIdAndKeyword(@Param("superId") Integer superId, @Param("subcompanyId") Integer subcompanyId,
                                                                           @Param("keyword") String keyword);

    @Select({
            "select * from department where id = #{id}"
    })
    Department selectPrimarykeyById(@Param("id") Integer id);

    @Select({
            "select * from department where corp_code = #{corpCode} and super_id = #{superId}"
    })
    Department findRootByCorpCode(@Param("corpCode") String corpCode, @Param("superId") Integer superId);

    @Select({
            "select * from department where super_id = #{superId} and subcompany_id = #{subcompanyId}"
    })
    List<Department> selectAllDepartmentsBySubaccountId(@Param("superId") Integer superId, @Param("subcompanyId") Integer subcompanyId);

    @InsertProvider(type= DepartmentProdiver.class, method = "insertSelective")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int add(Department department);
}
